package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalInfo;
import com.moleq.models.PosSetting;

public class PosSettingService {
	private DatabaseHelper DBHelper;
	private String errorString;
	private Context mContext;
	
	public PosSettingService() {

	}

	public PosSettingService(Context context) {
//		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(GlobalInfo.getInstance().ShareContext);
	}
	
//	public List<PosSetting> FindAll(){
//		List<PosSetting> obj = new ArrayList<PosSetting>();
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
//		Cursor cursor = db.rawQuery("select * from t_pos_setting",null);
//		while(cursor.moveToNext()){
//			PosSetting item = new PosSetting(
//					cursor.getInt(cursor.getColumnIndex("ps_id")),
//					cursor.getDouble(cursor.getColumnIndex("ps_station")),
//					cursor.getString(cursor.getColumnIndex("ps_open_time")),
//					cursor.getString(cursor.getColumnIndex("ps_close_time")),
//					cursor.getDouble(cursor.getColumnIndex("ps_tare_rate")),
//					cursor.getDouble(cursor.getColumnIndex("ps_tax1_rate")),
//					cursor.getDouble(cursor.getColumnIndex("ps_tax2_rate")),
//					cursor.getDouble(cursor.getColumnIndex("ps_tax3_rate")),
//					cursor.getDouble(cursor.getColumnIndex("ps_tax4_rate")),
//					cursor.getInt(cursor.getColumnIndex("ps_fc_max")),
//					cursor.getString(cursor.getColumnIndex("create_datetime")),
//					cursor.getString(cursor.getColumnIndex("update_datetime"))
//					);
//			obj.add(item);
//		}
//		cursor.close();
//		return obj;
//	}
	
	public long getRowCount() {
		long count = 0;
		try {
			SQLiteDatabase db = DBHelper.getWritableDatabase();
			String strSQL = "select count(*) from "+DatabaseSchema.PosSetting.SCHEMA_NAME ;
			Cursor cursor = db.rawQuery(strSQL, null);

			if (cursor.moveToFirst()) {
				count = cursor.getInt(0);
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	
	
	public PosSetting Find() {
		PosSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("SELECT * FROM " + DatabaseSchema.PosSetting.SCHEMA_NAME, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ps_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.ID));
			int ps_station = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.STATION));
			String ps_open_time = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.OPEN_TIME));
			String ps_close_time = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CLOSE_TIME));
			double ps_tare_rate = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosSetting.TARE_RATE));
			int ps_fc_max = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.FILPCHART_MAX));
			String create_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CREATE_DATETIME));
			String update_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.UPDATE_DATETIME));
			String ps_weight_unit  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.PS_WEIGHT_UNIT));
			int price_digits  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.PRICE_DIGITS)); 
			int store_no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.STORE_NO));
			String currency = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CURRENCY_SIGN));
			String paper = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.OUTOFPAPER));
			String calculate_weight = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CALCULATE_WEIGHT));
			String calendar_type = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CALENDAR_TYPE));
			String norm_upce = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.NORM_UPCE));
			String network_onoff = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.NETWORK_ONOFF));
			String show_desc = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.SHOW_DESC));
			String dvr = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.DVR));
			String zero_baln = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.ZERO_BALN));
			String zero_price = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.ZERO_PRICE));
			int fs_tax_rule  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.FS_TAX_RULE));
			int prctag_printer  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.PRCTAG_PRINTER));
			int rounding  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.ROUNDING));
			String print_service = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.PRINT_SERVICE));
			String sig_host = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.SIG_HOST));
			String sig_local = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.SIG_LOCAL));
			double ps_sig_min = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosSetting.SIG_MIN));
			int ps_receipt_pnt  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.RECEIPT_PNT));
			if (currency == null) {
				currency = "";
			}
			if (paper == null) {
				paper = "0";
			}
			obj = new PosSetting(ps_id, ps_station, ps_open_time, ps_close_time, ps_fc_max, create_datetime, update_datetime, 
					ps_weight_unit, price_digits, ps_tare_rate, store_no, currency,paper,calculate_weight,calendar_type, 
					norm_upce, network_onoff, show_desc, dvr, zero_baln, zero_price, fs_tax_rule, prctag_printer, rounding, 
					print_service, sig_host, sig_local, ps_sig_min, ps_receipt_pnt);
		}

		cursor.close();
		return obj;
	}
	
	public List<PosSetting> getAllPosSetting(){
		List<PosSetting> obj = new ArrayList<PosSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("SELECT * FROM " + DatabaseSchema.PosSetting.SCHEMA_NAME, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ps_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.ID));
			int ps_station = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.STATION));
			String ps_open_time = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.OPEN_TIME));
			String ps_close_time = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CLOSE_TIME));
			double ps_tare_rate = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosSetting.TARE_RATE));
			int ps_fc_max = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.FILPCHART_MAX));
			String create_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CREATE_DATETIME));
			String update_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.UPDATE_DATETIME));
			String ps_weight_unit  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.PS_WEIGHT_UNIT));
			int price_digits  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.PRICE_DIGITS)); 
			int store_no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.STORE_NO));
			String currency = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CURRENCY_SIGN));
			String paper = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.OUTOFPAPER));
			String calculate_weight = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CALCULATE_WEIGHT));
			String calendar_type = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.CALENDAR_TYPE));
			String norm_upce = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.NORM_UPCE));
			String network_onoff = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.NETWORK_ONOFF));
			String show_desc = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.SHOW_DESC));
			String dvr = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.DVR));
			String zero_baln = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.ZERO_BALN));
			String zero_price = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.ZERO_PRICE));
			int fs_tax_rule  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.FS_TAX_RULE));
			int prctag_printer  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.PRCTAG_PRINTER));
			int rounding  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.ROUNDING));
			String print_service = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.PRINT_SERVICE));
			String sig_host = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.SIG_HOST));
			String sig_local = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosSetting.SIG_LOCAL));
			double ps_sig_min = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosSetting.SIG_MIN));
			int ps_receipt_pnt  =cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosSetting.RECEIPT_PNT));
			if (currency == null) {
				currency = "";
			}
			if (paper == null) {
				paper = "0";
			}
			obj.add(new PosSetting(ps_id, ps_station, ps_open_time, ps_close_time, ps_fc_max, create_datetime, update_datetime, 
					ps_weight_unit, price_digits, ps_tare_rate, store_no, currency,paper,calculate_weight,calendar_type, 
					norm_upce, network_onoff, show_desc, dvr, zero_baln, zero_price, fs_tax_rule, prctag_printer, rounding, 
					print_service, sig_host, sig_local, ps_sig_min, ps_receipt_pnt));
		}
		cursor.close();
		return obj;
	}
	
	public boolean insertDefaultPos(){
		boolean check = true;
		String sql;
		sql = "INSERT INTO "+ DatabaseSchema.PosSetting.SCHEMA_NAME +" values(null,1,'100000','233000'," +
				"0.01,100,strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'),'lb',0,4,'','0'," +
				"'0','1','1','0','0','0','1','0',1,null,null,'0','0','0',null,0)";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean updatePosSetting(PosSetting obj){
		boolean check = true;
		String sql,open_time,close_time,unit,currency,paper,
				weight,calendar_type,norm_upce,network_onoff,
				show_desc,dvr,zero_baln,zero_price,
				printer,round,print_service,sig_host,sig_local;
		int id,station,fc_max,store_no,digits,fs_tax_rule,prctag_printer,rounding,receipt_pnt;
		double tare_rate,sig_min;
		id = obj.getPs_id();
		station = obj.getPs_station();
		open_time = obj.getPs_open_time();
		close_time = obj.getPs_close_time();
		tare_rate = obj.getPs_tare_rate();
		fc_max = obj.getPs_fc_max();
		unit = obj.getPs_weight_unit();
		store_no = obj.getStore_no();
		digits = obj.getPrice_digits();
		currency = obj.getCurrency_sign();
		paper = obj.getOutofpaper();
		weight = obj.getCalculate_weight();
		calendar_type = obj.getCalendar_type();
		norm_upce = obj.getNorm_upce();
		network_onoff = obj.getNetwork_onoff();
		show_desc = obj.getShow_desc();
		dvr = obj.getDvr();
		zero_baln = obj.getZero_baln();
		zero_price = obj.getZero_price();
		fs_tax_rule = obj.getPs_fs_tax_rule();
		prctag_printer = obj.getPs_prctag_printer();
		print_service = obj.getPs_print_service();
		sig_host = obj.getPs_sig_host();
		sig_local = obj.getPs_sig_local();
		receipt_pnt = obj.getPs_receipt_pnt();
		if (prctag_printer == -1) {
			printer = null;
		}else {
			printer = String.valueOf(prctag_printer);
		}
		rounding = obj.getPs_rounding();
		if (rounding == -1) {
			round = null;
		}else {
			round = String.valueOf(rounding);
		}
		sig_min = obj.getPs_sig_min();
		
		sql = "UPDATE "+ DatabaseSchema.PosSetting.SCHEMA_NAME +" set ";
		sql += "ps_station = "+ station +",ps_open_time = '"+ open_time +"',ps_close_time = '"+ close_time +"'," +
				"ps_tare_rate = "+ tare_rate +",ps_fc_max = "+ fc_max +",update_datetime = strftime('%Y%m%d%H%M%S','now')," +
				"ps_weight_unit = '"+ unit +"',ps_store_no = "+ store_no +",ps_price_digits = "+ digits +"," +
				"ps_currency_sign = '"+ currency +"',ps_out_of_paper = '"+ paper +"',ps_calc_weight = '" + weight + "'," +
				"ps_calendar_type = '"+ calendar_type +"',ps_norm_upce = '"+ norm_upce +"',ps_network_onoff = '"+ network_onoff +"'," +
				"ps_show_desc = '"+ show_desc +"',ps_dvr = '"+ dvr +"',ps_zero_baln = '"+ zero_baln +"'," +
				"ps_zero_price = '"+ zero_price +"',ps_fs_tax_rule = "+ fs_tax_rule +"," +
				"ps_prctag_printer = "+ printer +",ps_rounding = "+ round +"," +
				"ps_print_service = '"+ print_service +"',ps_sig_host = '"+ sig_host +"'," +
				"ps_sig_local = '"+ sig_local +"',ps_sig_min = "+ sig_min +",ps_receipt_pnt = " + receipt_pnt +" where ps_id = "+ id;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean updatePosNetwork(int id){
		boolean check = true;
		String sql;
		
		sql = "UPDATE "+ DatabaseSchema.PosSetting.SCHEMA_NAME +" set ";
		sql += "ps_network_onoff = '1' where ps_id = "+ id;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean deletePosSetting(int id){
		boolean check = true;
		String sql = "delete from "+ DatabaseSchema.PosSetting.SCHEMA_NAME +" where ps_id = " + id;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public Float getPosSettingRate(){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select ps_tare_rate from t_pos_setting", null);
		Float rate;
		while (cursor.moveToNext()) {
			rate = cursor.getFloat(0);
			cursor.close();
			return rate;
		}
		cursor.close();
		return (float) 0.00;
	}
	
	public int getPosSettingFCCount(){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select ps_fc_max from t_pos_setting", null);
		int fcCount = 0;
		while (cursor.moveToNext()) {
			fcCount = cursor.getInt(0);
			cursor.close();
			return fcCount;
		}
		cursor.close();
		return fcCount;
	}
}
